package com.genius.unite_platform.storedevice.service.impl;


import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.unite_platform.device.entity.DeviceEntity;
import com.genius.unite_platform.storedevice.entity.StoreDeviceEntity;
import com.genius.unite_platform.storedevice.service.StoreDeviceServiceAdapter;
import com.genius.unite_platform.storedevice.entity.StoreDeviceEntity;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.dingding.util.GeniusConfig;
import com.genius.workflow.element.SQLBean;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SearchResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;
import com.genius.workflow.util.DBTools;
import javax.servlet.http.HttpSession;

@Service("StoreDeviceInsertService")
public class StoreDeviceInsertServiceImpl extends StoreDeviceServiceAdapter{
	String CAIJIDATABASE = GeniusConfig.CAIJIDATABASE;  //jhcvs
	
	@Override
	public JSONObject add(JSONObject json, JSONObject response,HttpSession session) {    	
    	// 先查询主键是否存在
    	try {  
    		String keywords = "storeno,deviceno,modelId";
	    	String insertTable = "storedevice";
			JSONObject existJson = DBTools2.getIsExist(keywords,insertTable, json);			
			if(existJson != null){
				response.put(ResponseJson.msg,  json.getString("storeno")+ "已存在数据,不能新增！");
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				return response;
			}	
			
			JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);			
			json.put(DeviceEntity.lrr, userJson.getString(UserJson.name));		
				
			String itemField = "storeno,deviceno,modelId,devicenum,memo,imgs,lrr,lrsj";
			JSONArray jsonArray = new JSONArray();		
			for(int i=0;i<Integer.parseInt(json.getString("totalNum"));i++){				
				JSONObject jsonObject = new JSONObject();
				String deviceno=json.getString("deviceno_"+i);
				String flag=json.getString("flag_"+i);
				if (flag.equals("add"))
				{
				  if (!deviceno.equals(""))
			      {
			    	Date day=new Date();
			    	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
			     	jsonObject.put("storeno",json.getString("storeno"));
				    jsonObject.put("deviceno",json.getString("deviceno_"+i));
				    jsonObject.put("modelId",json.getString("modelId_"+i));	
				    jsonObject.put("devicenum",json.getString("devicenum_"+i));
			  //  	jsonObject.put("imgs",json.getString("img_"+i));	
				    jsonObject.put("imgs","");	
				    jsonObject.put("memo","");	
			    	jsonObject.put("lrr",userJson.getString(UserJson.name));	
				    jsonObject.put("lrsj",sdf.format(day));	
				    jsonArray.add(jsonObject);			  
				  }
				}
			}		
			System.out.println(jsonArray);
		    DBTools2.insertBatch(itemField,"storedevice",jsonArray);
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Error);
		}
		return response;	

    
	}
	
	@Override
	public JSONObject getDevice(JSONObject json, JSONObject response) {   
		try {
		JSONObject result = new JSONObject();		
		
		String selectField = "";
		String whereField = "";
		String table = "device";
		HashMap<String, String> sqlMap = new HashMap<>();
		
		sqlMap.put("device", "select deviceno,devicename from device order by deviceno ");
		sqlMap.put("devicemodel", "select a.deviceno,a.devicename,b.modelId,b.modelname,b.modeltypeId,b.modeltype  from  device a,devicemodel b  where a.deviceno=b.deviceno ");
		
		JSONObject fillJson = new JSONObject();
		
		result = DBTools.getFilling(selectField,whereField,table,json,result);
		JSONObject fillEditJson = DBTools.getFilling(sqlMap);
		fillJson.put(FillAddJson.field, result);
		fillJson.put(FillAddJson.select, fillEditJson);
	
		
		response.put(ResponseJson.status, ResponseJson.status_Success);
		response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Success);
		response.put(ResponseJson.data,fillJson);
	}  catch (ConnectionException e) {
		response.put(ResponseJson.status, ResponseJson.status_Failed);
		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
	} catch (DBClosedException e) {
		response.put(ResponseJson.status, ResponseJson.status_Failed);
		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
	} catch (SQLException e) {
		response.put(ResponseJson.status, ResponseJson.status_Failed);
		response.put(ResponseJson.msg, ResponseJson.msg_Edit_Fill_Data_Error);
	}		
	return response;
	
					
	}
	
	
	
	@Override
	public JSONArray getStore(String storeno) {   
	String selectsql = " select  deptcode,deptname from "+CAIJIDATABASE+".dict_department  where type=1 and  deptcode='"+storeno+"'";
   	JSONArray jsonarray = new JSONArray();
  	 try {
  		jsonarray = DBUtils.selectArr(selectsql,null);
	     } catch (SQLException e) {
		            e.printStackTrace();
		 }
         return jsonarray;
	}

}


